<template>
<div>
 <div class="app-page">
   <!-- <a :href="url" ca="1788_cxjh5_pack$点击大礼包" ><img src="../../../assets/image/share/pack.png" /></a> -->
   <a @click="initGiftState" ca="201788_cxjylhd_lblq$礼包领取页按钮" ><img src="../../../assets/image/share/pack.png" /></a>
   
 </div>
 <div class="loading" v-show="showLoading"></div>
 </div>
</template>
<script type="text/javascript">
import common from "../../../assets/js/common.js";
import {Toast, Popup} from 'mint-ui';
import Vue from 'vue';
Vue.component(Popup.name, Popup);
export default {
    data () {
        return {
           signUrl: process.env.NODE_ENV == 'sit' || process.env.NODE_ENV== 'development' ? common.isEnv("//recruit") : common.isEnv("//wx"),
           url: '../gifts/gifts.' + common.isHtml(),
           showLoading:false
        }
    },
    computed: {
    },
    mounted () {
      //打点
     /* this.$nextTick(function () {
      	common.ANA_AnalyticsScan();
      });*/
    },
    methods: {
      gotoUrl:function(){
          var _this = this;
          var giftPackCode = common.getQueryString("giftPackCode");//"c9b23915-2c78-4256-abcb-6fcd83ae6769";
          _this.$http.get(common.isdev() + "/service/x1788/coupon?giftPackCode="+giftPackCode).then(function(d) {
          var obj = JSON.parse(d.bodyText);
          console.log(obj.code===-99);
          var code = obj.code?obj.code:"",
          result = obj.result?obj.result:"",
          coupon = result.coupon?result.coupon:"",
          state = result.state?result.state:"";
          if(code==0){ 
                if(state == -2){
                    Toast("礼包已过期");
                }else if(state == -3){
                  Toast("活动已结束");
              }else if(state == -1){
                  Toast("今天的券已经抢完咯，明天请早点来哦")
              }else{
               window.location.href= window.location.protocol + "//" + window.location.host +"/cx/cxj/cxjweb/share/gifts/gifts.shtml?index="+coupon.idx+"&giftPackCode="+giftPackCode;
            }
          }else if(code == 1){
             _this.showLoading = false;
             Toast(obj.message);
          }
        }, function(err) {
           Toast(err);
        });
      },
      initGiftState:function(fn){
         var  _this = this;
         _this.showLoading = true;
         var giftPackCode = common.getQueryString("giftPackCode");//"c9b23915-2c78-4256-abcb-6fcd83ae6769";
         _this.$http.get(common.isdev() + "/service/x1788/user/state?giftPackCode="+giftPackCode).then(function(d) {
          var obj = JSON.parse(d.bodyText);
          var code = obj.code?obj.code:"",
          message = obj.message?obj.message:"",
          result = obj.result?obj.result:"",
          coupon = result.couponVO?result.couponVO:"";
          if(code==0){
             var state = result.state;
             if(state === 0){
               _this.showLoading = false;
                var backUrl = encodeURIComponent(window.location.href);
                console.log(backUrl);
                window.location.href = common.isdev() + "/service/error/user_not_logined?backUrl="+backUrl;
             }else if(state === 6){
              _this.showLoading = false;
                Toast("礼包已领完");
             }else if(state === 5||state=== 7){
               _this.showLoading = false;
                Toast("您已领取了分享礼包奖励");
                window.setTimeout(function(){
                  _this.gotoUrl();
                },2000)
             }else{
               _this.showLoading = false;
                 _this.gotoUrl(); 
             }
          }else if(code===1){
            _this.showLoading = false;
            Toast(message);
          }
        }, function(err) {
           Toast(err);
        });
      }
    } 
}  
  
</script>
<style lang="scss">
@import '../../../assets/css/common.scss';
@function rem($px){
  @return $px / 40 * 1rem;
}
img{width:100%;max-width:100%;height:auto;}
html,body{height:100%;overflow:hidden;}
body{background:url('../../../assets/image/share/pack_bg.jpg') center top no-repeat;background-size:100% 100%;max-width:750px;margin:0 auto;
  .app-page{padding:rem(130) rem(115) rem(420) rem(230);text-align:center;position:absolute;}
}
.loading{
  position:fixed;
  width:100%;
  height:100%;
  background:transparent url("../../../assets/image/loading.gif") no-repeat center center;
  /* background-color: rgba(11, 23, 43, 0.8); */
  background-size:rem(200) rem(200);
  z-index:600;
}
</style>

